<template>
	<view>
		<!-- 点击搜索建议过来的 -->
		<uni-search-bar @confirm="search" @input="input" placeholder="请输入搜索内容..." cancelButton="none" focus></uni-search-bar>
		<view v-if="suggestionList.length !== 0" class="suggestion">
			<view class="sug_item" v-for="(item, index) in suggestionList" :key="index" @click="toSearch(item)">
				<text class="sug_text">{{item.goods_name}}</text>
				<uni-icons type="arrowright" size="16"></uni-icons>
			</view>
		</view>
		<view class="history" v-else>
			<view class="history_title">
				<text>搜索历史</text>
				<uni-icons type="trash" size="16" @click="delShow=true"></uni-icons>
			</view>
			<view class="history_content">
				<view class="history_item" v-for="(item, index) in historyList" :key="index">
					<view class="history_item_text">{{item.goods_name}}</view>
					<uni-icons v-if="delShow" type="closeempty" size="12" class="delHistory" @click="delHistoryItem(index)"></uni-icons>
				</view>
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				timer: null,
				suggestionList: [],
				historyList: [],
				delShow: false
			};
		},
		onLoad() {
			this.historyList = JSON.parse(uni.getStorageSync('historyList') || '[]')
		},
		methods: {
			// 点击联想建议项
			toSearch(item) {
				console.log(item)
				this.keepHistory(item)
				uni.navigateTo({
				    url: '/subpkg/goods_detail/goods_detail?goods_id=' + item.goods_id
				})
			},
			// 回车搜索框
			search(value) {
				console.log(value)
				this.keepHistory({goods_name: value.value})
			},
			// 搜索框内容改变
			input(value) {
				if (value.length === 0) return this.suggestionList = []
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					console.log(value)
					this.getSuggestionList(value)
				}, 1000)
			},
			// 获取联想建议内容
			async getSuggestionList(kw) {
				const { data } = await uni.$http.get('/api/public/v1/goods/qsearch', {query: kw})
				if (data.meta.status !== 200) return uni.showMsg('加载失败')
				this.suggestionList = data.message
			},
			// 存储搜索记录
			keepHistory(value) {
				const index = this.historyList.findIndex(item => {
					return item === value
				})
				if (index !== -1) {
					this.historyList.splice(index, 1)
				}
				this.historyList.unshift(value)
				uni.setStorageSync('historyList', JSON.stringify(this.historyList))
			},
			// 删除历史
			delHistoryItem(index) {
				console.log(index)
				this.historyList.splice(index, 1)
				uni.setStorageSync('historyList', JSON.stringify(this.historyList))
				
			}
		}
	}
</script>

<style lang="scss">
	.sug_item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 0 20rpx;
		height: 50px;
		line-height: 50px;
		.sug_text {
			width: 90%;
			white-space: nowrap;
			overflow: hidden;
			text-overflow: ellipsis;
		}
		uni-icons {
			width: 10%;
			text-align: right;
		}
	}
	.history {
		.history_title {
			padding: 30rpx;
			display: flex;
			justify-content: space-between;
			align-items: center;
		}
		.history_content {
			display: flex;
			align-content: center;
			flex-wrap: wrap;
			.history_item {
				position: relative;
				width: 50px;
				height: 30px;
				line-height: 30px;
				text-align: center;
				background-color: #F0F0F0;
				border-radius: 5px;
				margin: 5px 9px;
				padding: 0 3px;
				.history_item_text {
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
				.delHistory {
					position: absolute;
					top: -5px;
					right: -3px;
					width: 16px;
					height: 16px;
					padding: 0;
					border-radius: 50%;
					line-height: 16px;
					text-align: center;
					background-color: #C0C0C0;
				}
			}
		}
	}

</style>
